/**
 * 
 */
package ua.kiev.ntukpi.asu.ga;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @author Alexander Galibey
 *
 */
public class PopulationGenerator {
	
	private static int MAX_DURATION = 10;
	
	private static int DEFAULT_THRESHOLD = 10;
	
	private PopulationGenerator() {
		
	}
	
	public static List<Individual> generate(int amount, int capacity){
		
		System.out.println("Starting generation of " + capacity + " individuals with " + amount + " jobs each.");
		long before = System.currentTimeMillis();
		List<Job> jobs = new ArrayList<Job>();
		Random random = new Random();
		for(int i = 0; i < amount; i++){
			
			Job job = new Job(i+1);
			job.setDuration(random.nextInt(MAX_DURATION) + 1);
			jobs.add(job);
		}
		
		generateDeadlines(jobs);

		List<Individual> result = generate(jobs, capacity);
		
		long after = System.currentTimeMillis();
		System.out.println("Generation completed. Elapsed time: " + (after - before) + " millis");
		return result;
	}
	
	public static List<Individual> generate(List<Job> jobs, int capacity){
		
		List<Individual> result = new ArrayList<Individual>();
		for (int i = 0; i < capacity; i++){
			
			Individual individual = new Individual();
			List<Job> individualJobs = new ArrayList<Job>(jobs);
			individual.setJobs(individualJobs);
			Collections.shuffle(individualJobs);
			result.add(individual);
			
		}
		return result;
	}
	
	private static void generateDeadlines(List<Job> jobs){
		
		int sumDurations = 0;
		Random random = new Random();
		for (Job job : jobs) {

			sumDurations += job.getDuration();
		}
		sumDurations += DEFAULT_THRESHOLD;
		for(Job job : jobs){
			
			job.setDeadline(random.nextInt(sumDurations) + 1);
		}
	}
}
